<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Category extends MX_Controller
{

    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->model('category/category_model');
    }

    public function index($id, $page = '')
    {
        $data = array();
        $id = intval($id);
        $page = (intval($page) != 0) ? intval($page) : 1;
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $this->load->language('landing/landing', $this->_langCode);
        $data['lang'] = $this->lang->line('landing_lang');
        if ($id <= 0) {
            show_404();
        }
        $tmpInfo = $this->category_model->getCategoryInfo($id, $langCode);
        if (!$tmpInfo) {
            show_404();
        } else {
            $this->layout->setMenuActive($id);
            $data['headTitle'] = $tmpInfo[0]['name'];
            $data['catInfo'] = $tmpInfo[0];
            $this->load->model('post/post_model');
            $this->load->library('Pagination');
            $numPost = $this->post_model->getNumPostByCatId($id);
            $config['base_url'] = base_url('chuyen-muc/' . $this->mycommon->convertToAlias($tmpInfo[0]['name']) . '_' . $tmpInfo[0]['id']);
            $config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
            $config['suffix'] = '.html';
            $config['total_rows'] = $numPost;
            $config['per_page'] = 10;
            $config['cur_page'] = $page;
            $config['use_page_numbers'] = TRUE;
            $config['first_link'] = '«';
            $config['last_link'] = '»';
            $config['cur_tag_open'] = '<a class="active-page">';
            $config['cur_tag_close'] = '</a>';
            $config['num_links'] = 4;
            $config['uri_segment'] = 4;
            $this->pagination->initialize($config);
            $start = ($page - 1) * $config['per_page'];
            $data['pagination'] = $this->pagination->create_links();
            $tmpPost = $this->post_model->getPostByCatId($id, $config['per_page'], $start, $langCode);
            $data['post'] = $tmpPost;
        }
        $this->layout->view('category/category_view', $data);
    }

}

?>
